﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;

namespace Needle.Utils.Extensions
{
    /// <summary></summary>
    public static class DateTimeExtensions
    {

        private static DateTimeFormatInfo CurrentDateTimeFormat
        {
            get { return Thread.CurrentThread.CurrentCulture.DateTimeFormat; }
        }

        private static Calendar CurrentCalendar
        {
            get { return CurrentDateTimeFormat.Calendar; }
        }
        /// <summary></summary>
        public static bool IsSameDate(this DateTime thisDate, DateTime date)
        {
          return  thisDate.Date == date.Date;
        }
        /// <summary></summary>
        public static bool IsSameWeek(this DateTime thisDate, DateTime date)
        {
            return thisDate.FirstDayOfWeek() == date.FirstDayOfWeek();
        }
        /// <summary></summary>
        public static bool IsSameMonth(this DateTime thisDate, DateTime date)
        {
            return thisDate.Year == date.Year && thisDate.Month == date.Month;
        }
        /// <summary></summary>
        public static bool IsSameYear(this DateTime thisDate, DateTime date)
        {
            return thisDate.Year == date.Year;
        }
        /// <summary></summary>
        public static DateTime FirstDayOfWeek(this DateTime thisDate)
        {
            DayOfWeek firstDay = CurrentDateTimeFormat.FirstDayOfWeek;
            DateTime date = thisDate;
            while (date.DayOfWeek != firstDay)
            {
                date = date.AddDays(-1);
            }
            return date.Date;
        }
        /// <summary></summary>
        public static DateTime LastDayOfWeek(this DateTime thisDate)
        {
            return thisDate.FirstDayOfWeek().AddDays(6);
        }
        /// <summary></summary>
        public static string LocalizedDayName(this DateTime thisDate)
        {
            return CurrentDateTimeFormat.DayNames[(int)thisDate.DayOfWeek];
        }
        /// <summary></summary>
        public static DateTime FirstDayOfMonth(this DateTime thisDate)
        {
            return new DateTime(thisDate.Year, thisDate.Month, 1);
        }
        /// <summary></summary>
        public static DateTime LastDayOfMonth(this DateTime thisDate)
        {
            return new DateTime(thisDate.Year, thisDate.Month, CurrentCalendar.GetDaysInMonth(thisDate.Year, thisDate.Month));
        }
    }
}
